package com.zizy.healthexpert.Database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

//T_FOODS_TYPE
public class MyDataBase_T_FOODS_TYPE {
	static final int PLACETOTAL = 115;// use for check if the table is
	// completely created
	static MyDataBase_T_FOODS_TYPE.DataBase myDataBaseHelper;
	private SQLiteDatabase db;// the order is name, type,latitude,longtitude,
	final static String TABLENAME = "T_FOODS_TYPE";

	public MyDataBase_T_FOODS_TYPE(Context context) {
		myDataBaseHelper = new MyDataBase_T_FOODS_TYPE.DataBase(context);
		this.db = myDataBaseHelper.getWritableDatabase();
		//initMyDataBase(db);
	}

	public void createTable(String tableName) {
		String sql = "create table if not exists "
				+ tableName
				+ " (RecNo integer primary key autoincrement, _ID varchar(20) not null ,_NAME varchar(20) not null );";
		db.execSQL(sql);
	}

	// init database
	public void initMyDataBase(SQLiteDatabase db) {
		int placeTotal = 0;
		this.createTable(TABLENAME);
		if (this.SearchT_FOODS_TYPE("*") != null)
			placeTotal = this.SearchT_FOODS_TYPE("*").size();
		// if is completely create then return
		if (placeTotal == MyDataBase_T_FOODS_TYPE.PLACETOTAL)
			return;
		// or insert
		//myDataBaseHelper.deleteTable();
		this.createTable(TABLENAME);

		this.Insert(1, 1, "穀類");

	}

	public void deleteTabel() {

	}

	// insert operating
	public void Insert(int RecNo, int _ID, String _NAME) {

		this.db.execSQL(String.format(
				"insert into %s (RecNo,_ID, _NAME) values('%s','%s','%s')",
				TABLENAME, RecNo, _ID, _NAME));
	}

	// update operation
	public void Update(String field, Object value, String where_expression) {
		this.db.execSQL(String.format("update %s set password=%s where %s ;",
				TABLENAME, field, value, where_expression));
	}

	// Delete operation
	public void Delete(String where_expression) {
		this.db.execSQL(String.format("delete from %s  where %s ;", TABLENAME,
				where_expression));
	}

	public ArrayList<MyDataBase_T_FOODS_TYPE.T_FOODS_TYPE> SearchT_FOODS_TYPE(
			String selectWhat) {
		ArrayList<MyDataBase_T_FOODS_TYPE.T_FOODS_TYPE> FOODSList = new ArrayList<MyDataBase_T_FOODS_TYPE.T_FOODS_TYPE>();
		Cursor cursor = db.rawQuery("select " + selectWhat + " from "
				+ TABLENAME, null);
		while (cursor.moveToNext()) {
			int _ID = cursor.getInt(0);
			String _NAME = cursor.getString(1);

			// here create a Place class to add info.
			FOODSList.add(new T_FOODS_TYPE(   _ID, _NAME));

		}
		cursor.close();

		return FOODSList;
	}

	public ArrayList<MyDataBase_T_FOODS_TYPE.T_FOODS_TYPE> SearchT_FOODS_TYPE(
			String selectWhat, String where_expression) {
		ArrayList<MyDataBase_T_FOODS_TYPE.T_FOODS_TYPE> FOODSList = new ArrayList<MyDataBase_T_FOODS_TYPE.T_FOODS_TYPE>();
		Cursor cursor = db.rawQuery(String.format(
				"select %s from %s where %s;", selectWhat, TABLENAME,
				where_expression), null);
		while (cursor.moveToNext()) {

			int _ID = cursor.getInt(0);
			String _NAME = cursor.getString(1);

			FOODSList.add(new T_FOODS_TYPE(  _ID, _NAME));

		}
		cursor.close();
		return FOODSList;
	}

	public void Close() {
		this.db.close();
	}

	public class DataBase extends DatabaseHelper {
		public DataBase(Context context) {

			super(context );
			// TODO Auto-generated constructor stub
		}

	}

	// here is a Type of Place
	public class T_FOODS_TYPE {

		public int _ID;
		public String _NAME;

		T_FOODS_TYPE(  int _ID, String _NAME) {

			this._ID = _ID;
			this._NAME = _NAME;

		}

	}

}
